//
//  CarCalculatorViewModel.m
//  CarUser
//
//  Created by ayi on 2019/1/24.
//  Copyright © 2019 王森林. All rights reserved.
//




#import "CarCalculatorViewModel.h"

#import "Custom_dznEmptyDelegate.h"
@interface CarCalculatorViewModel()

@property (nonatomic, strong) Custom_dznEmptyDelegate *custom_emptyView;
//1、首付，2、分期利率，3、购置税，4、车船使用税，5、交强险，6、商业保险
@property (nonatomic, strong) NSMutableArray<PZLXDMModel *> *PZLXDM_1;
@property (nonatomic, strong) NSMutableArray<PZLXDMModel *> *PZLXDM_2;

@property (nonatomic, strong) PZLXDMModel *PZLXDM_3;
@property (nonatomic, strong) PZLXDMModel *PZLXDM_4;
@property (nonatomic, strong) PZLXDMModel *PZLXDM_5;
@property (nonatomic, strong) PZLXDMModel *PZLXDM_6;

@property (nonatomic, strong) NSMutableArray<PZLXDMModel *> *PZLXDM_7;//第三者责任险

@property (nonatomic, strong) PZLXDMModel *PZLXDM_8;//基础保费
@property (nonatomic, strong) PZLXDMModel *PZLXDM_9;//全车盗抢险费率
@property (nonatomic, strong) NSMutableArray<PZLXDMModel *> *PZLXDM_10;//车身划痕险
@property (nonatomic, strong) NSMutableArray<PZLXDMModel *> *custom_glass_PZLXDM;//玻璃单独破碎险

@end

@implementation CarCalculatorViewModel

@synthesize childtab = _childtab;
@synthesize childScroll = _childScroll;

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        self.responseModel = [[BaseNetResponseModel alloc]init];
        [self raccommandInitFirst];
        
    }
    return self;
}

- (void)setChildtab:(UITableView *)childtab{
    _childtab = childtab;
    
    @weakify(self);
    _custom_emptyView = [[Custom_dznEmptyDelegate alloc]initWithEmptyTitle:@"暂无数据" andImgName:nil andBtnName:nil andverticalOffset:0 andScroll:_childtab andCallBack:nil andFreshCallBack:^{
        @strongify(self);
        [self.raccommand execute:@(1)];
    } andOtherCallBack:nil];
    _custom_emptyView.adminationStr = @"加载中...";
}

- (void)setChildScroll:(UIScrollView *)childScroll{
    _childScroll = childScroll;
    
    @weakify(self);
    _custom_emptyView = [[Custom_dznEmptyDelegate alloc]initWithEmptyTitle:nil andImgName:nil andBtnName:nil andverticalOffset:-40 andScroll:_childScroll andCallBack:nil andFreshCallBack:^{
        @strongify(self);
        [self.raccommand execute:@(1)];
    } andOtherCallBack:nil];
    _custom_emptyView.adminationStr = @"加载中...";
}


- (void)raccommandInitFirst{
    //获取数据
    @weakify(self);
    self.raccommand = [[RACCommand alloc]initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        @strongify(self);
        RACSignal *sign = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            
            [self.custom_emptyView willbeginStartAdmination];
            
            [[ZY_AfnNetManager shareInstance]postWithURL:Car_API_getComputeConfigAll params:nil md5Paraname:nil request:nil response:self.responseModel success:^(id  _Nullable response) {
                
                __block NSMutableArray *list = [NSMutableArray arrayWithCapacity:0];
                [((NSArray *)response[@"list"]) enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    PZLXDMModel *onemodel = [PZLXDMModel yy_modelWithDictionary:obj];
                    [list addObject:onemodel];
                }];
                //1、首付，2、分期利率，3、购置税，4、车船使用税，5、交强险，6、商业保险
                NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"PXSZ01" ascending:YES];
                self.PZLXDM_1 = [self selectedPZLXDM:list andType:@"1"].mutableCopy;
                [self.PZLXDM_1 sortUsingDescriptors:@[sort]];
                self.PZLXDM_2 = [self selectedPZLXDM:list andType:@"2"].mutableCopy;
                [self.PZLXDM_2 sortUsingDescriptors:@[sort]];
                self.PZLXDM_3 = [self selectedPZLXDM:list andType:@"3"].firstObject;
                self.PZLXDM_4 = [self selectedPZLXDM:list andType:@"4"].firstObject;
                self.PZLXDM_5 = [self selectedPZLXDM:list andType:@"5"].firstObject;
                self.PZLXDM_6 = [self selectedPZLXDM:list andType:@"6"].firstObject;
                
                self.PZLXDM_7 = [self selectedPZLXDM:list andType:@"7"].mutableCopy;
                [self.PZLXDM_7 sortUsingDescriptors:@[sort]];
                self.PZLXDM_8 = [self selectedPZLXDM:list andType:@"8"].firstObject;
                self.PZLXDM_9 = [self selectedPZLXDM:list andType:@"9"].firstObject;
                self.PZLXDM_10 = [self selectedPZLXDM:list andType:@"10"].mutableCopy;
                [self.PZLXDM_10 sortUsingDescriptors:@[sort]];
                
                PZLXDMModel *model1 = [[PZLXDMModel alloc]init];
                model1.PZMC01 = @"进口";
                model1.PXSZ02 = 0.0025;
                PZLXDMModel *model2 = [[PZLXDMModel alloc]init];
                model2.PZMC01 = @"国产";
                model2.PXSZ02 = 0.0015;
                self.custom_glass_PZLXDM = @[model1,model2].mutableCopy;
                
                self.PZLXDM_1.firstObject.hasChoose = YES;
                self.PZLXDM_2.firstObject.hasChoose = YES;
                self.PZLXDM_7.firstObject.hasChoose = YES;
                self.PZLXDM_10.firstObject.hasChoose = YES;
                self.custom_glass_PZLXDM.firstObject.hasChoose = YES;
                self.carMoney = self.carmodel?(double)([self.carmodel.ZDJG00 floatValue] * 10000):0;
                
                self.custom_emptyView.responseModel = nil;
                [self.childtab reloadData];
                [self resetType2Data];
                self.currentType = 1;
                [subscriber sendNext:@"ok"];
                [subscriber sendCompleted];
                
            } fail:^(NSError * _Nonnull error) {
                
                self.custom_emptyView.responseModel = self.responseModel;
                [subscriber sendCompleted];
            }];
            
            return nil;
        }];
        return sign;
    }];
    
    //监听
    [[RACObserve(self, currentType) skip:1]subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [self freshHeaderTtile];
    }];
    
    [[[RACObserve(self, carMoney) skip:1] throttle:0.2] subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [self freshHeaderTtile];
    }];
}
#pragma mark ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------处理数据
- (NSArray *)selectedPZLXDM:(NSArray *)list andType:(NSString *)indexStr{
    if (list == nil || list.count == 0) {
        return nil;
    }
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"PZLXDM=%@",indexStr]];
    return [list filteredArrayUsingPredicate:predicate];
}

- (NSArray *)selectedPZLXDM:(NSArray *)list andpredicatest:(NSString *)predicatestr{
    if (list == nil || list.count == 0) {
        return nil;
    }
    NSPredicate *predicate = [NSPredicate predicateWithFormat:predicatestr];
    return [list filteredArrayUsingPredicate:predicate];
}

//- (void)resetType1Data{
//    CGFloat carMoney = (double)([self.carmodel.ZDJG00 floatValue] * 10000);
//    CGFloat gouzhi = 0;
//    CGFloat chechuanshui = self.PZLXDM_4.PXSZ02 ;
//    CGFloat jiaoqingxian = self.PZLXDM_5.PXSZ02;
//    CGFloat shangyebaoxian = 0;
//
//    [self freshHeaderTtile];
//    self.yuegongtext = [NSString stringWithFormat:@""];
//    self.duohuafeitext = [NSString stringWithFormat:@""];
//    self.zonghuafeitext = [NSString stringWithFormat:@""];
//    self.loanPeriodContenttext = [NSString stringWithFormat:@""];
//    self.downPaymentContenttext = [NSString stringWithFormat:@""];
//    self.needToSpendContenttext = [CarCalculatorViewModel stringChangeMoneyWithDouble:(long)(gouzhi + chechuanshui + jiaoqingxian)];
//    self.insuranceContenttext = [CarCalculatorViewModel stringChangeMoneyWithDouble:(long)(shangyebaoxian)];;
//}

- (void)resetType2Data{
    if (self.carmodel == nil) {
        return;
    }
    CGFloat carMoney = self.carMoney;
    CGFloat gouzhi = self.PZLXDM_3.PXSZ01 * carMoney;
    CGFloat chechuanshui = self.PZLXDM_4.PXSZ01;
    CGFloat jiaoqingxian = self.PZLXDM_5.PXSZ01;
    CGFloat shangyebaoxian = self.PZLXDM_6.PXSZ01 * carMoney;
    
    [self freshHeaderTtile];
//    self.yuegongtext = [NSString stringWithFormat:@""];
//    self.duohuafeitext = [NSString stringWithFormat:@""];
//    self.zonghuafeitext = [NSString stringWithFormat:@""];
    
    [self loanPeriodBtnTouch:[self.PZLXDM_2 filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"hasChoose=true"]].firstObject];
    [self downPaymentBtnTouch:[self.PZLXDM_1 filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"hasChoose=true"]].firstObject];
    
    self.needToSpendContenttext = [CarCalculatorViewModel stringChangeMoneyWithDouble:(long)(gouzhi + chechuanshui + jiaoqingxian)];
    self.insuranceContenttext = [CarCalculatorViewModel stringChangeMoneyWithDouble:(long)(shangyebaoxian)];
    
    self.gouzhishuiContenttext = [CarCalculatorViewModel stringChangeMoneyWithDouble:(long)(gouzhi)];
    self.chechuanshuiContenttext = [CarCalculatorViewModel stringChangeMoneyWithDouble:(long)(chechuanshui)];
    self.qiangxianContenttext = [CarCalculatorViewModel stringChangeMoneyWithDouble:(long)(jiaoqingxian)];
    self.linpaiContenttext = @"以实际发生费用为准";
    self.shangjiaoContenttext = @"以实际发生费用为准";
}

//贷款期限
- (void)loanPeriodBtnTouch:(PZLXDMModel *)model{
    self.loanPeriodContenttext = model.PZMC01;
    [self freshHeaderMore];
}
//裸车首付
- (void)downPaymentBtnTouch:(PZLXDMModel *)model{
    CGFloat carMoney = self.carMoney;
    
    self.downPaymentContenttext = [NSString stringWithFormat:@"%@(%@)",model.PZMC01,[CarCalculatorViewModel stringChangeMoneyWithDouble:(long)(carMoney * model.PXSZ01)]];
    [self freshHeaderMore];
}

//刷新贷款
- (void)freshHeaderMore{
    //贷款
    PZLXDMModel *insmodel = [self.PZLXDM_2 filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"hasChoose=true"]].firstObject;
    //首付
    PZLXDMModel *downpaymodel = [self.PZLXDM_1 filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"hasChoose=true"]].firstObject;
    
    CGFloat carMoney = (double)([self.carmodel.ZDJG00 floatValue] * 10000);
    CGFloat insmoney = carMoney * (1 - downpaymodel.PXSZ01);
    CGFloat moreMoney = insmoney * insmodel.PXSZ02 * insmodel.PXSZ01 / 12;
    self.yuegongtext = [self CustomAttrubuteString:[CarCalculatorViewModel stringChangeMoneyWithDouble:(long)roundf(((insmoney + moreMoney) / insmodel.PXSZ01))] andTitle:@"月供(元)"];
    self.duohuafeitext = [self CustomAttrubuteString:[CarCalculatorViewModel stringChangeMoneyWithDouble:(long)roundf((moreMoney))] andTitle:@"多花费(元)"];
    self.zonghuafeitext = [self CustomAttrubuteString:[CarCalculatorViewModel stringChangeMoneyWithDouble:(long)roundf(moreMoney + carMoney)] andTitle:@"总花费(元)"];
    
    [self freshHeaderTtile];
}

- (NSMutableAttributedString *)CustomAttrubuteString:(NSString *)str andTitle:(NSString *)title{
    NSMutableAttributedString *str1 = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@\n",str] attributes:@{
                                                                                                        NSFontAttributeName:[UIFont boldSystemFontOfSize:14],
                                                                                                        NSForegroundColorAttributeName:Common_TextColor
                                                                                                        }];
    NSMutableAttributedString *str2 = [[NSMutableAttributedString alloc]initWithString:title attributes:@{
                                                                                                        NSFontAttributeName:[UIFont systemFontOfSize:12],
                                                                                                        NSForegroundColorAttributeName:Common_TextColor
                                                                                                        }];
    [str1 appendAttributedString:str2];
    return str1;
}

- (NSMutableAttributedString *)CustomAttrubuteString2:(NSString *)str andTitle:(NSString *)title{
    NSMutableAttributedString *str1 = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@",str] attributes:@{
                                                                                                                                            NSFontAttributeName:[UIFont boldSystemFontOfSize:25],
                                                                                                                                            NSForegroundColorAttributeName:UIColor.blackColor
                                                                                                                                            }];
    NSMutableAttributedString *str2 = [[NSMutableAttributedString alloc]initWithString:title attributes:@{
                                                                                                          NSFontAttributeName:[UIFont systemFontOfSize:12],
                                                                                                          NSForegroundColorAttributeName:UIColor.blackColor
                                                                                                          }];
    [str1 appendAttributedString:str2];
    return str1;
}


//头部设置
- (void)freshHeaderTtile{
    CGFloat shangyebaoxian =  [self moreDataInit];
    if (self.carmodel == nil) {
        return;
    }
    CGFloat carMoney = self.carMoney;
    CGFloat gouzhi = self.PZLXDM_3.PXSZ01 * carMoney;
    CGFloat chechuanshui = self.PZLXDM_4.PXSZ01;
    CGFloat jiaoqingxian = self.PZLXDM_5.PXSZ01;
    
    
    if (self.currentType == 1) {
        self.headertext1 = [NSString stringWithFormat:@"预计总花费(裸车价格+必要花费+商业保险)"];
        NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
        formatter.positiveFormat = @",###.##"; // 正数格式
        NSString *money = [formatter stringFromNumber:@((carMoney + gouzhi + chechuanshui + jiaoqingxian + shangyebaoxian))];
        self.headertext2 = [self CustomAttrubuteString2:money andTitle:@" 元"];
        
    }else{
        self.headertext1 = [NSString stringWithFormat:@"预计总花费(裸车首付+必要花费+商业保险)"];
        PZLXDMModel *model = [self.PZLXDM_1 filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"hasChoose=true"]].firstObject;
        
        NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
        formatter.positiveFormat = @",###.##"; // 正数格式
        NSString *money = [formatter stringFromNumber:@((carMoney * model.PXSZ01 + gouzhi + chechuanshui + jiaoqingxian + shangyebaoxian))];
        self.headertext2 = [self CustomAttrubuteString2:money andTitle:@" 元"];
    }
}

//20190311 zzy增加
- (CGFloat )moreDataInit{
    CGFloat carMoney = self.carMoney;
    
    @autoreleasepool {
        PZLXDMModel *model = [self selectedPZLXDM:self.PZLXDM_7 andpredicatest:@"hasChoose=true"].firstObject;
        PZLXDMModel *model1 = [self selectedPZLXDM:self.custom_glass_PZLXDM andpredicatest:@"hasChoose=true"].firstObject;
        PZLXDMModel *model2 = [self selectedPZLXDM:self.PZLXDM_7 andpredicatest:@"hasChoose=true"].firstObject;
        PZLXDMModel *model3 = [self selectedPZLXDM:self.PZLXDM_10 andpredicatest:@"hasChoose=true"].firstObject;
        CGFloat m_1 = model.PXSZ01;
        CGFloat m_2 = self.PZLXDM_8.PXSZ02 + carMoney * 0.01088;
        CGFloat m_3 = self.PZLXDM_8.PXSZ02 + carMoney * self.PZLXDM_9.PXSZ02;
        CGFloat m_4 = model1.PXSZ02 * carMoney;
        CGFloat m_5 = 0.0015 * carMoney;
        CGFloat m_6 = (self.PZLXDM_8.PXSZ02 + carMoney * 0.01088 + self.PZLXDM_7.firstObject.PXSZ01) * 0.2;
        CGFloat m_7 = model2.PXSZ01 * 0.2;
        CGFloat m_8 = 50;
        CGFloat m_9 = model3.PXSZ02;
        
        self.vaule_more_1 =  [CarCalculatorViewModel stringChangeMoneyWithDouble:m_1];
        self.vaule_more_2 =  [CarCalculatorViewModel stringChangeMoneyWithDouble:m_2];
        self.vaule_more_3 =  [CarCalculatorViewModel stringChangeMoneyWithDouble:m_3];
        self.vaule_more_4 =  [CarCalculatorViewModel stringChangeMoneyWithDouble:m_4];
        self.vaule_more_5 =  [CarCalculatorViewModel stringChangeMoneyWithDouble:m_5];
        self.vaule_more_6 =  [CarCalculatorViewModel stringChangeMoneyWithDouble:m_6];
        self.vaule_more_7 =  [CarCalculatorViewModel stringChangeMoneyWithDouble:m_7];
        self.vaule_more_8 =  [CarCalculatorViewModel stringChangeMoneyWithDouble:m_8];
        self.vaule_more_9 =  [CarCalculatorViewModel stringChangeMoneyWithDouble:m_9];
        self.vaule_more_all =  [CarCalculatorViewModel stringChangeMoneyWithDouble:(m_1 + m_2 + m_3+ m_4+ m_5+ m_6+ m_7+ m_8+ m_9)];
        return (m_1 + m_2 + m_3+ m_4+ m_5+ m_6+ m_7+ m_8+ m_9);
    }
    
}

- (void)moreDataInitMore{
    [self freshHeaderTtile];
}





+ (NSString *)stringChangeMoneyWithDouble:(double)number {
    
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    formatter.positiveFormat = @",###.##"; // 正数格式
    NSString *money = [formatter stringFromNumber:@(number)];
    return [NSString stringWithFormat:@"%@ 元",money];
}


@end












@implementation PZLXDMModel



@end


